package com.chenyu.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.*;

@Component
public class JDBCBeanUtil {

    private DataSource datasource;
    private Connection getConn() {
        Connection conn = null;
        try {
            conn = datasource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    public Integer getAll() {
        Connection conn = getConn();
        String sql = "select * from user";
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            System.out.println("开始============================");
            while (rs.next()) {
                for (int i = 1; i <= col; i++) {
                    System.out.print(rs.getString(1) + "\t");
                }

            }
            System.out.println("结束===========================");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null != rs){
                    rs.close();
                }
                if(null != pstmt){
                    pstmt.close();
                }
                if(null != conn){
                    conn.close();
                }
            }catch (Exception ec){
                ec.printStackTrace();
            }
        }
        return null;
    }
}
